package com.example.tool.activtiy.base;

import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.example.tool.R;
import com.example.tool.mvp.base.BasePresenter;
import com.example.tool.mvp.base.IBaseView;
import com.gyf.immersionbar.ImmersionBar;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Create by liangkun on 2018/11/22
 * Describe :基类MVPactvtiy,针对activtiy含有fragment，适配沉浸式
 */

public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity implements IBaseView {
    protected P presenter;
    private Unbinder unbinder;
    public Context mContext;

    @SuppressWarnings("unchecked")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//强制竖屏
        setContentView(getLayoutId());
        ImmersionBar.with(this)
                .transparentStatusBar()  //透明状态栏，不写默认透明色
                .transparentNavigationBar()//透明导航栏，不写默认黑色(设置此方法，fullScreen()方法自动为true)
                .statusBarColor(R.color.color_white)
                .fitsSystemWindows(true)
                .statusBarDarkFont(true)   //状态栏字体是深色，不写默认为亮色
                .navigationBarColor(R.color.color_e2dfdd) //导航栏颜色，不写默认黑色
//                .navigationBarDarkIcon(true)//导航栏图标是深色，不写默认为亮色;
                .navigationBarAlpha(0.001f)//导航栏透明度，不写默认0.0F
                .init();
        unbinder = ButterKnife.bind(this);
        mContext = this;
        //创建present
        presenter = createPresenter();
        if (presenter != null) {
            presenter.attachView(this);
        }
        initView();
        initData();
        initEvent();

    }


    protected abstract int getLayoutId();

    protected abstract void initView();

    protected abstract void initData();

    protected abstract void initEvent();


    protected abstract P createPresenter();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.detachView();
            presenter = null;
        }
        unbinder.unbind();
    }

    /**
     * 带参数跳转界面
     *
     * @param context 当前页面
     * @param classz
     */
    public void startToActivity(Context context, Class classz) {
        Intent intent = new Intent(context, classz);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            context.startActivity(intent, ActivityOptions.makeSceneTransitionAnimation((Activity) context).toBundle());
        } else {
            context.startActivity(intent);
        }
    }


}
